#include "ADUPipe.h"


PipeCircle* PipeCircle::createPipeCircle(const string& file) {
	PipeCircle* sprite = new PipeCircle();
	if(sprite && sprite->initWithFile(file)) {
		sprite->autorelease();
        return sprite;
    }
    CC_SAFE_DELETE(sprite);
    return nullptr;
}

void PipeCircle::enableTouch() {
	mTouchListener = EventListenerTouchOneByOne::create();
//	mTouchListener->setSwallowTouches(true);
	mTouchListener->onTouchBegan = CC_CALLBACK_2(PipeCircle::onTouchBegan, this);
	mTouchListener->onTouchMoved = CC_CALLBACK_2(PipeCircle::onTouchMoved, this);
	mTouchListener->onTouchEnded = CC_CALLBACK_2(PipeCircle::onTouchEnded, this);
	mTouchListener->onTouchCancelled = CC_CALLBACK_2(PipeCircle::onTouchCancelled, this);
	_eventDispatcher->addEventListenerWithSceneGraphPriority(mTouchListener, this);

}

bool PipeCircle::onTouchBegan(Touch *touch, Event *event) {
	if(this->getBoundingBox().containsPoint(touch->getLocation())) {
		mCircleTouch = true;
		mPipeHeadTouch = false;
	} else if(mPipeHead && mPipeHead->getBoundingBox().containsPoint(touch->getLocation())) {
		mCircleTouch = false;
		mPipeHeadTouch = true;	
	} else {
		mCircleTouch = false;
		mPipeHeadTouch = false;	
	}
	return true;
}

void PipeCircle::onTouchMoved(Touch* touch, Event* event) {
	if(mCircleTouch) {
		auto deltaX = touch->getDelta().x;
		auto deltaY = touch->getDelta().y;
		auto curX = this->getPositionX();
		auto curY = this->getPositionY();
		this->setPosition(curX + deltaX, curY + deltaY);
		if(mPipeLine) {
			auto x = mPipeLine->getPositionX();
			auto y = mPipeLine->getPositionY();
			mPipeLine->setPosition(x + deltaX, y + deltaY);
		}
		
		if(mPipeHead) {
			auto x = mPipeHead->getPositionX();
			auto y = mPipeHead->getPositionY();
			mPipeHead->setPosition(x + deltaX, y + deltaY);
		}
	} else if(mPipeHeadTouch) {
		// Rotate pipe head
		auto diffPoint = mPipeHead->getPosition() - touch->getLocation();
		auto angleRadians = atan2f(diffPoint.y,diffPoint.x);
		mPipeHead->setRotation(90 + angleRadians * 180 / M_PI);
	}
}
void PipeCircle::onTouchEnded(Touch* touch, Event* event) {
	mCircleTouch = false;
	mPipeHeadTouch = false;	
}

void PipeCircle::onTouchCancelled(Touch* touch, Event* event) {
}

/////////////////// END PIPE_CIRCLE /////////////////////////////////////////////////////////


PipeHead* PipeHead::createPipeHead(const string& file) {
	PipeHead* sprite = new PipeHead();
	if(sprite && sprite->initWithFile(file)) {
		sprite->setAnchorPoint(Vec2(0,0.5));
		sprite->autorelease();
        return sprite;
    }
    CC_SAFE_DELETE(sprite);
    return nullptr;
}


/////////////////// END PIPE_HEAD /////////////////////////////////////////////////////////

void Pipe::createPipe(Node * _layer) {
	auto 	_visibleSize = Director::getInstance()->getVisibleSize();
    auto 	_origin = Director::getInstance()->getVisibleOrigin();
    auto 	_centerX = _origin.x + _visibleSize.width/2;
    auto 	_centerY = _origin.y + _visibleSize.height/2;

	mPipeLine = SpriteBatchNode::create(IMG_PIPE_LINE,NUM_PIPE_LINE);
	mPipeLine->setPosition(Vec2(0,_centerY));
	
	auto 	_winSize = Director::getInstance()->getWinSize();
	auto 	_widthScreen = _winSize.width;
	auto 	_heightScreen = _winSize.height;
	auto 	_pipeSize = mPipeLine->getContentSize();
	auto 	_pipeWidth = _pipeSize.width;
	auto 	_pipeHeight = _pipeSize.height;
	auto 	_anchorPoint = mPipeLine->getAnchorPoint();
	auto 	_pipeSegWidth = mPipeLine->getTexture()->getContentSize().width;
	
	// Add PipeLine

	for(int i = 0; i < NUM_PIPE_LINE; i++) 	
	{		
		auto _pipeSegX = _pipeSegWidth/2 + i * _pipeSegWidth;
		auto _pipeSegY = _pipeHeight * _anchorPoint.y;
		if(i == NUM_PIPE_LINE - 1) {
			mLastSegPos.x = _pipeSegX;
			mLastSegPos.y = _pipeSegY;
		}
		mPipeSeg[i] = Sprite::createWithTexture(mPipeLine->getTexture());
		mPipeSeg[i]->setPosition(Vec2(_pipeSegX,_pipeSegY));
		mPipeLine->addChild(mPipeSeg[i]);
	}
	_layer->addChild(mPipeLine, Z_PIPE);
	

	// Add PipeCircle
	mPipeCircle = PipeCircle::createPipeCircle(IMG_PIPE_CIRCLE);
	float _circleWidth = mPipeCircle->getContentSize().width;
	mPipeCircle->setPosition(Vec2(mLastSegPos.x + _circleWidth,mPipeLine->getPositionY()));
	_layer->addChild(mPipeCircle,Z_PIPECIRCLE);

	// Add PipeHead
	mPipeHead = PipeHead::createPipeHead(IMG_PIPE_HEAD);
	mPipeHead->setPosition(mPipeCircle->getPositionX() + _circleWidth/4 ,mPipeLine->getPositionY());
	_layer->addChild(mPipeHead,Z_PIPE);

	mPipeCircle->setPipeHead(mPipeHead);
	mPipeCircle->setPipe(mPipeLine);
	mPipeCircle->enableTouch();
}

